"Thirty days has November. April, June, and September. Of twenty eight is but one. And all the rest are thirty one. Of course leap year comes and slays. Every four years got it right, and twenty eight is twenty nine." ―Calendar Man
Calendar Man has always been one of the more fascinating Batman villains. A highly intelligent individual who plans all his crimes with reference to his calendar and the important dates that human civilisation has attached to these days and months. Clearly, Calendar Man sees the importance of time in his framework of crime, and this no different for someone studying data.
time series
noun
"A series of values of a quantity obtained at successive times, often with equal intervals between them."
There are plenty of instances in which the observed data contains a temporal element. Applying time series analysis can provide applicative functions to areas such as:
Something here about decomposing the time series
# Basic packages
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import random as rd # generating random numbers
import datetime # manipulating date formats
# Viz
import matplotlib.pyplot as plt # basic plotting
import seaborn as sns # for prettier plots
import yfinance as yf
# TIME SERIES
from statsmodels.tsa.arima_model import ARIMA
from statsmodels.tsa.statespace.sarimax import SARIMAX
from pandas.plotting import autocorrelation_plot
from statsmodels.tsa.stattools import adfuller, acf, pacf,arma_order_select_ic
import statsmodels.formula.api as smf
import statsmodels.tsa.api as smt
import statsmodels.api as sm
import scipy.stats as scs
from statsmodels.tsa.seasonal import seasonal_decompose
# settings
import warnings
warnings.filterwarnings("ignore")
plt.rcParams['figure.figsize'] = (20.0, 10.0)
plt.style.use("seaborn-deep")
In Construction: We will be using Apple Stock Data to illustrate some of the key methods and applications of various time series anlysis methods.
apple = yf.Ticker("AAPL")
apple = apple.history(period="1m",start="2000-1-1",end="2020-1-1")
# Importing with yfinance does not set frequency. Important when inputting to other models.
apple = apple.asfreq('B',method='ffill')
Some information here regarding moving averages
One method... Simple Moving Average
apple["Close"].plot(figsize=(16,10),title="Apple Closing Prices from 2000 to 2010",label='Apple')
apple["Close"].rolling(window=200).mean().plot(legend="200 month moving average",label="200 MA")
plt.legend()
None
apple["Close"].pct_change().plot(figsize=(16,10),title="Apple Returns")
# decomposes the apple stock returns
result = (seasonal_decompose(pd.DataFrame(apple["2000":]['Close']),period=365))
fig = result.plot()
# group by month (with names using the month_name method), on the Close column and find the mean of each month, sort and plot values
apple.groupby(apple.index.month_name(),)["Close"].mean().sort_values().plot.bar(rot=45,ylabel="Apple Closing Price",title="Mean Closing Price by Month From 1999-2020")